#include "zip_test_set.h"

test_set_t *
test_set_create()
{
    test_set_t *set = malloc(sizeof(test_set_t));
    if (!set)
        return NULL;
    set->header = NULL;
    set->count = 0;

    return set;
}
void
test_set_destroy(test_set_t * test_set)
{
    test_node_t *node = test_set->header;
    while(node)
    {
        test_set->header = test_set->header->next;
        test_node_free(node);
    }
    free(test_set);
}
test_node_free(test_node_t *test_node)
{
    pkg_list_destroy(test_node->pkg_list);
    free(test_node);
}
pkg_list_t *
test_node_get_data(test_node_t *test_node)
{
    return test_node->pkg_list;
}
void
test_node_set_data(test_node_t *test_node, pkg_list_t *pkg_list)
{
    test_node->pkg_list = pkg_list;
}

test_node_t *
test_set_insert(test_set_t *test_set, wire_name_t *wire_name)
{
    test_node_t *node = test_set->header;
    while (node)
    {
        if (0 == wire_name_compare(node->wire_name, wire_name))
            break;
        node = node->next;
    }
    if (node)
    {
        return node;
    }
    else
    {
        test_node_t * new_node = malloc(sizeof(test_node_t));
        if (!new_node)
            return NULL;
        new_node->wire_name = wire_name;
        new_node->pkg_list = NULL;
        new_node->next = test_set->header;
        test_set->header = new_node;
        test_set->count += 1;
        return new_node;
    }

}
void
test_set_delete(test_set_t *test_set, wire_name_t *wire_name)
{
    ASSERT(test_set && wire_name, "set delete parameter is NULL");
    char domain_name[512];
    wire_name_to_string(wire_name, domain_name);

    test_node_t *node = test_set->header;
    test_node_t *prev = NULL;
    while(node)
    {
        wire_name_to_string(node->wire_name, domain_name);
        if (0 == wire_name_compare(node->wire_name, wire_name))
            break;
        prev = node;
        node = node->next;
    }
    if (node)
    {
        if (!prev)
            test_set->header = node->next;
        else
            prev->next = node->next;
        test_set->count -= 1;
        test_node_free(node);
    }
}

pkg_list_t *
test_set_search(test_set_t *test_set, wire_name_t *wire_name)
{
    test_node_t *node = test_set->header;
    test_node_t *prev = NULL;
    while(node)
    {
        if (0 == wire_name_compare(node->wire_name, wire_name))
            break;
        prev = node;
        node = node->next;
    }
    if (!node)
        return NULL;
    
    return node->pkg_list;
}
